﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace Evan.WebPageUI
{
    /// <summary>
    /// 前端基类
    /// </summary>
    public class Base : System.Web.UI.Page
    {
        /// <summary>
        /// 底层的逻辑类
        /// </summary>
        public BLL.Base Evan_BLL = new BLL.Base();

        #region  通用属性集合
        /// <summary>
        /// token验证字符串
        /// </summary>
        public string Token = "";
        /// <summary>
        /// 站点配置
        /// </summary>
        public dynamic SiteConfig = new Evan.Model.DynamicModel(); //站点配置
        /// <summary>
        /// 系统配置
        /// </summary>
        public dynamic SystemConfig = new Evan.Model.DynamicModel(); //系统配置
        
        /// <summary>
        /// 当前栏目ID
        /// </summary>
        public int ClassId = 0; // 栏目ID
        /// <summary>
        /// 默认分页大小 WebConfig/AppSetting/DefaultPageSize  中设置
        /// </summary>
        public int curPageSize = int.Parse(Evan.Common.CommFun.GetAppSetting("DefaultPageSize")); //默认分页大小
        /// <summary>
        /// 当前第几页  默认1
        /// </summary>
        public int curPage = 1; //当前第几页
        /// <summary>
        /// 当前内容的ID
        /// </summary>
        public int curId = 0; //当前内容的ID
        /// <summary>
        /// 当前内容的GUID
        /// </summary>
        public string curGUID = ""; //当前内容的GUID
        /// <summary>
        /// 当前页面的所属表
        /// </summary>
        public string curTable = ""; //当前页面的所属表

        /// <summary>
        /// 栏目的数据操作实例
        /// </summary>
        public Evan.DataBase.DbOperat BLL_Channel = new Evan.DataBase.DbOperat("Channel"); //栏目的数据操作实例
        /// <summary>
        /// 当前页面的数据操作实例
        /// </summary>
        public Evan.DataBase.DbOperat curBLL; //当前页面的数据操作实例

        /// <summary>
        /// 当前页是否可开启可视化编辑
        /// </summary>
        public bool Page_Is_vEdit = false;

        /// <summary>
        /// 是否admin账号
        /// </summary>
        public bool IsAdmin = false; //是否admin账号

        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsHomePage = false;
        /// <summary>
        /// 网站资源版本号
        /// </summary>
        public string SiteVersion = string.Empty;
        #endregion

        #region 内置当前栏目属性
        /// <summary>
        /// 当前栏目的排序
        /// </summary>
        public string curChannelOrderBy = "";
        /// <summary>
        /// 当前栏目是否顶级栏目
        /// <para>如果当前栏目是顶级栏目 则parentChannel 为 null</para> 
        /// </summary>
        public bool curChannelIsTopLevel = false;
        #endregion

        #region 内置各级关联栏目
        /// <summary>
        /// 顶级栏目
        /// </summary>
        public dynamic topLevelChannel = new Evan.Model.DynamicModel();
        /// <summary>
        /// 父级栏目
        /// </summary>
        public dynamic parentChannel = new Evan.Model.DynamicModel();
        /// <summary>
        /// 当前栏目
        /// </summary>
        public dynamic curChannel = new Evan.Model.DynamicModel();

        /// <summary>
        /// 父级栏目List
        /// <para>只查询IsIndexNav=1 [栏目设置导航显示]</para> 
        /// </summary>
        public List<dynamic> parentChannelList = new List<dynamic>();
        /// <summary>
        /// 当前栏目List 同级栏目列表
        /// <para>只查询IsIndexNav=1 [栏目设置导航显示]</para> 
        /// </summary>
        public List<dynamic> curChannelList = new List<dynamic>();
        /// <summary>
        /// 子级栏目List
        /// <para>只查询IsIndexNav=1 [栏目设置导航显示]</para> 
        /// </summary>
        public List<dynamic> childChannelList = new List<dynamic>();
        /// <summary>
        /// 顶级栏目List
        /// <para>通常用于主导航显示</para> 
        /// <para>只查询IsIndexNav=1 [栏目设置导航显示]</para> 
        /// </summary>
        public List<dynamic> topChannelList = new List<dynamic>();

        /// <summary>
        /// 所有栏目的集合 未区分 是否导航显示
        /// </summary>
        public List<dynamic> allChannelList = new List<dynamic>();

        #endregion

        
        /// <summary>
        /// Base 构造
        /// </summary>
        public Base()
        {
            Token = Common.CommFun.TokenGenerate(true);
            SiteConfig = Evan.Common.SysComm.GetSiteConfig();
            SiteVersion = GetStaticVersion();

            SystemConfig = Common.SysComm.GetSystemConfig();

            int.TryParse(Evan.Common.CommFun.GetQueryString("channel"), out ClassId);
            int.TryParse(Evan.Common.CommFun.GetQueryString("page"), out curPage);
            int.TryParse(Evan.Common.CommFun.GetQueryString("id"), out curId);

            curGUID = Evan.Common.CommFun.GetQueryString("guid");

            //获取所有栏目的集合 , 从数据库中获取
            allChannelList = GetAllChannelLists(true); //BLL_Channel.GetList(string.Format("States=1"), "SortId asc,Id asc");
            topChannelList = allChannelList.FindAll(res => res.ParentId == 0 && res.IsIndexNav == 1);

            if (ClassId > 0)
            {
                //当前栏目的内容
                curChannel = GetCurChannelById(ClassId);
                //curChannel = GetChannelById(ClassId);
                if (curChannel == null)
                {
                    throw new Exception("获取当前栏目属性出错 , 当前栏目为空 , 当前栏目ID为"+ClassId);
                }
                //当前栏目对应的表
                curTable = Evan.Template.TemplateHelper.GetTableNameByPageType(Convert.ToInt32(curChannel.PageType));
                //如果当前分页设置大于1的话 使用栏目分页设置
                if (curChannel.PageSize > 1)
                {
                    curPageSize = curChannel.PageSize;
                }
                //当前栏目的表操作实例
                curBLL = new Evan.DataBase.DbOperat(curTable);

                //自动判断单页类型 , 拿到单页的ID
                if (Convert.ToInt32(curChannel.PageType) == 3)
                {
                    //拿到单页的内容
                    dynamic _curModel_Temp = curBLL.GetModel_1(" ClassId=@ClassId and States=1 ", ClassId);
                    if (_curModel_Temp == null)
                    {
                        //单页内容为空则说明是增加 , Id 为 0
                        curId = 0;
                    }
                    else
                    {
                        curId = _curModel_Temp.Id;
                    }
                }

                //当前栏目的排序SQL语句
                curChannelOrderBy = Evan.Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
                topLevelChannel = GetTopLevelChannel(curChannel);

                //如果当前栏目是顶级栏目
                if (curChannel.ParentId == 0)
                {
                    curChannelIsTopLevel = true;
                    parentChannel = null;
                }
                else
                {
                    parentChannel = GetFatherLevelChannel(curChannel);
                    parentChannelList = GetFatherLevelChannelList(curChannel);
                }

                childChannelList = GetChildLevelChannel(curChannel, true);
                curChannelList = GetSameLevelChannel(curChannel, true);
                
            }
            else
            {
                //如果是首页打开  配置默认属性
                curChannel.Set("Id", 0);
                curChannel.Set("Title", "首页");
                curChannel.Set("ParentId", 0);

                topLevelChannel.Set("Id", 0);
                topLevelChannel.Set("Title", "首页");
                topLevelChannel.Set("ParentId", 0);
            }
            
            this.Load += new EventHandler(Base_Load);

        }

        void Base_Load(object sender, EventArgs e)
        {
            IsAdmin = Member.Administrator.IsAdminLogin();
            Page_Is_vEdit = Evan.Plugins.VisualEdit.vEditRole(Evan.Common.CommFun.GetQueryString("vedit"), ClassId, curId);
        }

        /// <summary>
        /// 不建议使用了 , 底层获取列表的时候写了对应的查询判断
        /// 获取定时发布的SQL语句
        /// </summary>
        /// <param name="curTableName">当前表名称 , 不区分大小写 , 如果不填则默认为当前表</param>
        /// <param name="andStr">and符号 , 默认是有and 的</param>
        /// <returns></returns>
        [Obsolete("不建议使用了 , 底层获取列表的时候写了对应的查询判断",true)]
        public string GetReleaseTimeSqlStr(string curTableName = "", string andStr = " and ")
        {
            if (string.IsNullOrEmpty(curTableName))
            {
                curTableName = curTable;
            }
            bool has = Evan_BLL.GetHasReleaseTime(curTableName.ToLower());  
            if (has) { return andStr + " ReleaseTime<GetDate() "; }
            else { return ""; }
        }


        #region  公司标识
        /// <summary>
        /// 获取沙漠风版权标识
        /// </summary>
        /// <param name="HasSmwind">是否包含风享 默认包含[true]</param>
        /// <param name="Ver">版本 默认 cn[中文]  hk或tw 为繁体   en 为英文版</param>
        /// <param name="CusCont">自定义显示内容 , 如果有自定义显示内容 , 则显示自定义显示内容</param>
        /// <param name="ShowDate">显示时间  格式 yyyy-MM-dd [2018-12-31] , 此项优先级最高 如果时间不到 则返回空  注意 : 时间必须介于DateTime对象的区间 , 如 2000-01-01</param>
        /// <param name="BeforeCont">在返回内容前加的内容 通常可能是分隔符</param>
        /// <param name="AfterCont">在返回内容后加的内容 通常可能是分隔符</param>
        /// <returns></returns>
        public string GetSzweb(bool HasSmwind = true, string Ver = "cn", string CusCont = "", string ShowDate = "", string BeforeCont = "", string AfterCont = "")
        {
            string res = "";

            if (!string.IsNullOrEmpty(ShowDate))
            {
                if (DateTime.Now < Convert.ToDateTime(ShowDate))
                {
                    return res;
                }
            }

            if (!string.IsNullOrEmpty(CusCont))
            {
                return BeforeCont + CusCont + AfterCont;
            }

            if (Ver == "cn")
            {
                if (HasSmwind)
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">深圳网站建设 : 沙漠风</a> | <a href=\"http://www.smwind.com/\" target=\"_blank\">深圳APP开发 : 风享</a>";
                }
                else
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">深圳网站建设 : 沙漠风</a>";
                }
            }
            else if (Ver == "hk" || Ver == "tw")
            {
                if (HasSmwind)
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">深圳網站建設 : 沙漠風</a> | <a href=\"http://www.smwind.com/\" target=\"_blank\">深圳APP開發 : 風享</a>";
                }
                else
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">深圳網站建設 : 沙漠風</a>";
                }
            }
            else
            {
                if (HasSmwind)
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">Design By : szweb.cn</a> | <a href=\"http://www.smwind.com/\" target=\"_blank\">APP Design: smwind.com</a>";
                }
                else
                {
                    res = "<a href=\"http://www.szweb.cn/\" target=\"_blank\">Design By : szweb.cn</a>";
                }
            }
            return BeforeCont + res + AfterCont;
        }
        #endregion

        #region  通用方法
        /// <summary>
        /// 文本域中的 \n 换行符 替换
        /// <para>默认替换为 <br/> 换行</para>
        /// <para>也可以自定义替换的符号</para>
        /// </summary>
        /// <param name="Str">输入值</param>
        /// <param name="ReplaceStr">替换值</param>
        /// <returns></returns>
        public string TextAreaWrap(string Str, string ReplaceStr = "<br/>")
        {
            return Str.Replace("\n", ReplaceStr);
        }
        #endregion

        #region 可视化编辑使用到的方法
        //新建一个Model , 用于前端 vEdit方法 , 包括 id classid type field table default
        //default  用于添加的时候默认值 , 可以在后台定义 , 继承基类中增加default的调用 .   type 换成 字段

        /// <summary>
        /// 可视化编辑器属性 , 此功能依赖Jquery , 请务必保证页面上使用了Jquery
        /// <para>此方法调用 Evan.Plugins.VisualEdit.vEdit 做了一些默认参数值的处理</para> 
        /// <para>Id 不填 默认读取当前页的Id </para>    <para>ClassId 不填 默认读取当前页的ClassId</para>    <para>Table 不填默认读取curTable</para> 
        /// <para>curChannel 不填 默认读取当前页的curChannel </para>    <para>curOperat 不填 默认读取当前页的curBLL</para>
        /// 可视化编辑页面需调用 #include file="/App_Data/Template/vEditor.html"
        /// </summary>
        /// <param name="Attr">可视化编辑属性实体 </param>
        /// <returns></returns>
        public string vEdit(Evan.Plugins.VisualEdit.vEditAttr Attr)
        {
            if (!Page_Is_vEdit)
            {
                return "";
            }
            if (Attr.Id == 0)
            {
                Attr.Id = curId;
            }
            if (Attr.ClassId == 0)
            {
                Attr.ClassId = ClassId;
            }
            if (string.IsNullOrEmpty(Attr.Table))
            {
                Attr.Table = curTable;
            }
            if (Attr.curChannel==null)
            {
                Attr.curChannel = curChannel;
            }
            if (Attr.curOperat == null)
            {
                Attr.curOperat = curBLL;
            }
            return Evan.Plugins.VisualEdit.vEdit(Attr);
        }

        #endregion
        

        #region 获取和某个栏目相关的数据 
        /// <summary>
        /// 获取所有的栏目
        /// </summary>
        /// <param name="fromDataBase">是否从数据库获取</param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public List<dynamic> GetAllChannelLists(bool fromDataBase = false, bool IsIndexNav = false)
        {


            if (fromDataBase)
            {
                if (IsIndexNav)
                {
                    return new Evan.DataBase.DbOperat("Channel").GetList("States=1 and IsIndexNav=1", "SortId asc,Id asc");
                }
                else
                {
                    return new Evan.DataBase.DbOperat("Channel").GetList("States=1", "SortId asc,Id asc");
                }
            }
            else
            {
                return allChannelList;
            }
        }

        /// <summary>
        /// 获取某个栏目的同级栏目id集合
        /// <para>可指定使用curChannel.Id 或者 curChannel.ParentId</para>
        /// <para>将会获得某个栏目同级栏目的ID集合 , 通常用于查询当前栏目的ID列表</para>
        /// </summary>
        /// <param name="_curId">栏目ID</param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public string GetCurChannelIdList(int _curId, bool IsIndexNav = false)
        {
            dynamic channel = GetCurChannelById(_curId); //获取栏目对象
            List<dynamic> channelList = GetSameLevelChannel(channel,IsIndexNav); //拿到同级列表
            string idList = "";
            foreach (var item in channelList)
            {
                idList += item.Id + ",";
            }
            idList = idList.Trim(',');
            return idList;
        }
        /// <summary>
        /// 获取某个栏目的子级栏目id集合
        /// <para>将会获得某个栏目子级栏目的ID集合 , 通常用于查询获取某个栏目下的信息</para>
        /// </summary>
        /// <param name="_curId">栏目ID</param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public string GetChildChannelIdList(int _curId, bool IsIndexNav = false)
        {
            dynamic channel = GetCurChannelById(_curId); //获取栏目对象
            List<dynamic> channelList = GetChildLevelChannel(channel,IsIndexNav); //拿到子级列表
            string idList = "";
            foreach (var item in channelList)
            {
                idList += item.Id + ",";
            }
            idList = idList.Trim(',');
            return idList;
        }

        /// <summary>
        /// 获取某一个栏目
        /// </summary>
        /// <param name="_curId">传入栏目ID</param>
        /// <returns></returns>
        public dynamic GetCurChannelById(int _curId)
        {
            return GetAllChannelLists().Find((dynamic res) => res.Id == _curId);
        }

        /// <summary>
        /// 递归获取顶级栏目
        /// 数据从allChannelList中来 , 避免重复查询数据库
        /// </summary>
        /// <param name="_curChannel">栏目dynamic</param>
        /// <returns></returns>
        public dynamic GetTopLevelChannel(dynamic _curChannel)
        {
            if (_curChannel.ParentId > 0)
            {
                _curChannel = GetAllChannelLists().Find((dynamic res) => res.Id == _curChannel.ParentId);
                return GetTopLevelChannel(_curChannel);
            }
            else
            {
                return _curChannel;
            }
        }
        /// <summary>
        /// 递归获取顶级栏目
        /// </summary>
        /// <param name="ChannelId">栏目ID</param>
        /// <returns></returns>
        public dynamic GetTopLevelChannel(int ChannelId)
        {
            if (ChannelId > 0)
            {
                dynamic SingleChannel = allChannelList.Find((dynamic channel) => { return channel.Id == ChannelId; });
                while (SingleChannel.ParentId > 0)
                {
                    SingleChannel = allChannelList.Find((dynamic channel) => { return channel.Id == SingleChannel.ParentId; });
                }
                return SingleChannel;
            }
            return null;
        }

        /// <summary>
        /// 获取当前栏目的同级栏目
        /// </summary>
        /// <param name="_curChannel"></param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public List<dynamic> GetSameLevelChannel(dynamic _curChannel, bool IsIndexNav = false)
        {
            if (IsIndexNav)
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curChannel.ParentId && res.IsIndexNav == 1);
            }
            else
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curChannel.ParentId);
            }
        }
        /// <summary>
        /// 获取某个栏目ID 的同级栏目
        /// </summary>
        /// <param name="_curId"></param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public List<dynamic> GetSameLevelChannel(int _curId, bool IsIndexNav = false)
        {
            if (IsIndexNav)
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curId && res.IsIndexNav == 1);
            }
            else
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curId);
            }
        }

        /// <summary>
        /// 获取当前栏目的父栏目
        /// </summary>
        /// <param name="_curChannel"></param>
        /// <returns></returns>
        public dynamic GetFatherLevelChannel(dynamic _curChannel)
        {
            return GetAllChannelLists().Find((dynamic res) => res.Id == _curChannel.ParentId);
        }
        /// <summary>
        /// 获取某个栏目的父栏目
        /// </summary>
        /// <param name="_curId">传入的应该是</param>
        /// <returns></returns>
        public dynamic GetFatherLevelChannel(int _curId)
        {
            return GetAllChannelLists().Find((dynamic res) => res.Id == _curId);
        }

        /// <summary>
        /// 获取当前栏目的父栏目
        /// </summary>
        /// <param name="_curChannel"></param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public List<dynamic> GetFatherLevelChannelList(dynamic _curChannel, bool IsIndexNav = false)
        {
            if (IsIndexNav)
            {
                return GetAllChannelLists().FindAll(res => res.Id == _curChannel.ParentId && res.IsIndexNav == 1);
            }
            else
            {
                return GetAllChannelLists().FindAll(res => res.Id == _curChannel.ParentId);
            }
        }
        /// <summary>
        /// 获取当前栏目的子级栏目
        /// </summary>
        /// <param name="_curChannel"></param>
        /// <param name="IsIndexNav">是否只取导航显示 false=全部读取</param>
        /// <returns></returns>
        public List<dynamic> GetChildLevelChannel(dynamic _curChannel, bool IsIndexNav = false)
        {
            if (IsIndexNav)
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curChannel.Id && res.IsIndexNav == 1);
            }
            else
            {
                return GetAllChannelLists().FindAll(res => res.ParentId == _curChannel.Id);
            }
        }

        /// <summary>
        /// 获取当前栏目下第一个有连接的栏目的链接
        /// </summary>
        /// <param name="curChannel">当前栏目</param>
        /// <returns></returns>
        public string GetHasUrlChannel(dynamic curChannel)
        {
            if (string.IsNullOrEmpty(curChannel.WebPath))
            {
                //读取当前栏目的子栏目集合 不使用数据库查询 
                List<dynamic> ChannelList = allChannelList.FindAll(res => res.ParentId == curChannel.Id);
                //List<dynamic> ChannelList = allChannelList.FindAll(res => res.ParentId == curChannel.Id && res.IsIndexNav == 1);

                //new Evan.DataBase.DbOperat("Channel").GetList_1(0, "ParentId=@0 and States=1", "SortId asc", curChannel.id);
                if (ChannelList.Count > 0)
                {
                    return GetHasUrlChannel(ChannelList[0]);
                }
                else
                {
                    return "javascript:;";
                }
            }
            else
            {
                return curChannel.WebPath;
            }
        }
        #endregion

        #region 面包屑
        private List<dynamic> GetBreadCrumbsList(dynamic ThisChannel, List<dynamic> ChannelList)
        {
            if (ThisChannel.ParentId == 0)
            {
                //当前是顶级栏目了
                ChannelList.Add(ThisChannel);
                return ChannelList;
            }
            else
            {
                //当前对象传出去
                ChannelList.Add(ThisChannel);
                //拿到上级栏目的对象传出去
                ThisChannel = GetCurChannelById(ThisChannel.ParentId);
                GetBreadCrumbsList(ThisChannel, ChannelList);
            }
            return ChannelList;
        }
        /// <summary>
        /// 获取面包屑
        /// </summary>
        /// <param name="ThisChannel">当前栏目</param>
        /// <param name="ClassName">设置面包屑每个链接的class样式名称</param>
        /// <param name="HasLink">面包屑是否有链接</param>
        /// <param name="IsNewWind">是否需要在新窗口打开 默认不用</param>
        /// <param name="Separator">面包屑的连接符 , 默认 > </param>
        /// <param name="CurClassName">当前栏目的样式 , 默认 cur </param>
        /// <returns></returns>
        public string GetBreadCrumbsNav(dynamic ThisChannel, string ClassName = "", bool HasLink = true, bool IsNewWind = false, string Separator = " > ",string CurClassName = "cur")
        {
            List<dynamic> ChannelList = GetBreadCrumbsList(ThisChannel, new List<dynamic>());
            ChannelList.Reverse();
            string BreadCrumbsNavStr = "";
            int count = 0;
            foreach (var item in ChannelList)
            {
                count = count + 1;
                BreadCrumbsNavStr += "<a id=\"breadcrumbsnav-" + item.Id + "\" data-channe-id=\"" + item.Id + "\" data-channe-parent-id=\"" + item.ParentId + "\" class=\"" + ClassName + " " + (ThisChannel.Id == item.Id ? CurClassName : "") + "\" href=\"" + (HasLink ? (string.IsNullOrEmpty(item.WebPath) ? "javascript:;" : item.WebPath) : "javascript:;") + "\"  " + (IsNewWind ? "target=\"_blank\"" : "") + "  >" + item.Title + "</a>" + (ChannelList.Count == count ? "" : Separator);
            }
            return BreadCrumbsNavStr;
        }
        #endregion

        #region 根据具体内容ID获取对应的URL
        /// <summary>
        /// 根据当前model获取对应的内容链接
        /// default , list , detail , singlepage
        /// pageNum是获取分页的链接 , linkType 为 list 的时候才可以使用 
        /// 主要用于获取详细页链接
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hasSuffix">是否强制带上 页面名称  默认不用 ( 对 第一页/栏目页/单页生效)</param>
        /// <param name="linkType">default , list , detail , singlepage , 默认是detail</param>
        /// <param name="pageNum">默认是0 如果是其他页的话就传页码进来</param>
        /// <returns></returns>
        public string GetUrlByModel(dynamic model, bool hasSuffix=false, string linkType = "detail", int pageNum = 0)
        {
            string linkurl = "javascript:;";

             //传进来的可能是null 比如：在获取上一条数据时，可能存在null
            if (model==null) return linkurl;

            //根据model 拿到对应的栏目
            dynamic thischannel = GetCurChannelById(model.ClassId); //GetChannelByClassId(model.ClassId);

            //防止栏目被锁定或者删除，导致程序获取伪静态报错
            if (thischannel.States != 1) return linkurl;


            //拿到这个栏目的内容
            if (linkType.ToLower() == "singlepage")
            {
                //单页 , 直接返回栏目目录
                linkurl = thischannel.WebPath;
                if (hasSuffix)
                {
                    linkurl = linkurl + "index.html";
                }
            }
            if (linkType.ToLower() == "default")
            {
                //综合页  ,  直接返回栏目目录
                linkurl = thischannel.WebPath;
                if (hasSuffix)
                {
                    linkurl = linkurl + "default.html";
                }
            }
            if (linkType.ToLower() == "list")
            {
                //列表页 按照分页查看
                if (pageNum > 0)
                {
                    if (!string.IsNullOrEmpty(thischannel.RewriteList))
                    {
                        linkurl = thischannel.WebPath + ((thischannel.RewriteList).Replace("{Page}", pageNum.ToString()));
                    }
                }
                else
                {
                    linkurl = thischannel.WebPath;
                    if (hasSuffix)
                    {
                        linkurl = linkurl + "index.html";
                    }
                }
            }
            if (linkType.ToLower() == "detail")
            {
                //如果没有填详细页链接的话 , 返回空链接
                if (!string.IsNullOrEmpty(thischannel.RewriteDetail))
                {
                    linkurl = thischannel.WebPath;
                    //详细页
                    //先拿到伪静态配置列表 , 然后遍历列表 , 对比当前拿到的是那个正则名称 , 然后在将model转为dic 在里面拿属性 即可
                    foreach (var item in GetRewriteFieldList())
                    {
                        if ((thischannel.RewriteDetail).Contains("{" + item + "}"))
                        {
                            //如果存在值
                            Dictionary<string, object> dic = Evan.Model.ConvertModel.ConvertToDictionary(model);
                            linkurl = linkurl + ((thischannel.RewriteDetail).Replace("{" + item + "}", dic[item].ToString()));
                            break;
                        }
                    }
                }

            }
            
            return linkurl;
        }
        /// <summary>
        /// 从ext.config中获取伪静态字段 
        /// </summary>
        /// <returns></returns>
        private List<string> GetRewriteFieldList()
        {
            List<string> itemList = new List<string>();
            object itemList_obj = Evan.Common.CacheHelp.GetValue("RewriteFieldList");
            if (itemList_obj == null)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(Evan.Common.DirFile.ReadFile("~/App_Data/SysConfig/ext.config"));
                XmlNode xn = xmldoc.SelectSingleNode(string.Format("/root/extended[@type='{0}']", "rewrite-field-reg"));
                XmlNodeList nodelist = xn.ChildNodes;  //得到该节点的子节点
                if (nodelist.Count > 0)
                {
                    foreach (XmlElement el in nodelist)//读元素值
                    {
                        //Evan.SystemLog.MemLog.AddExLog2("读取ext.config", null, nodelist.Count.ToString());
                        //读取所有的
                        itemList.Add(el.Attributes["key"].Value);
                        //if (el.Attributes["type"].Value == "Field")
                        //{
                        //    itemList.Add(el.Attributes["key"].Value);
                        //}
                    }
                    if (itemList.Count > 0)
                    {
                        //存缓存
                        Evan.Common.CacheHelp.Insert_NoAbsolute_Rank("RewriteFieldList", itemList, Evan.Common.CacheHelp.CacheTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable);
                    }
                }
            }
            else
            {
                itemList = (List<string>)itemList_obj;
            }

            return itemList;
        }
        #endregion

        /// <summary>
        /// 获取某个栏目的所有属性
        /// 通过查询数据库获取
        /// 通常不建议使用此方法
        /// </summary>
        /// <param name="Id">栏目ID</param>
        /// <returns></returns>
        public dynamic GetChannelById(int Id)
        {
            return BLL_Channel.GetModel(Id);
        }


        /// <summary>
        /// 根据当前栏目获取Banner , 由当前层级往上递归查询
        /// <para>贡献者 : 胡法</para>
        /// </summary>
        /// <param name="CurChannel">当前栏目</param>
        /// <param name="DefaultImgUrl">默认图</param>
        /// <returns></returns>
        public string GetBannerImgUrl(dynamic CurChannel, string DefaultImgUrl = "")
        {
            string BannerImgUrl = DefaultImgUrl;
            if (CurChannel != null)
            {
                if (string.IsNullOrEmpty(CurChannel.ImgUrl))
                {
                    if (CurChannel.ParentId != 0)
                    {
                        dynamic ParentChannel = allChannelList.Find(res => res.Id == CurChannel.ParentId && res.States == 1);
                        if (ParentChannel != null)
                        {
                            return GetBannerImgUrl(ParentChannel, DefaultImgUrl);
                        }
                    }
                }
                else
                {
                    BannerImgUrl = CurChannel.ImgUrl;
                }
            }
            return BannerImgUrl;
        }


        /// <summary>
        /// 根据栏目ID获取当前栏目的URL
        /// 如果是顶级栏目的话  默认找第一个有URL的子级
        /// 如果不传defaultUrl的话, 默认返回 javascript:;
        /// </summary>
        /// <param name="id"></param>
        /// <param name="defaultUrl"></param>
        /// <returns></returns>
        [Obsolete]
        public string GetUrl(int id, string defaultUrl = null)

        {
            string webpath = string.Empty;
            var curchannel = allChannelList.Find(a => a.Id == id && a.States == 1);//获取当前栏目
            if (curchannel != null)
            {
                if (string.IsNullOrEmpty(curchannel.WebPath))//当前栏目没有填写路径
                {
                    //根据当前栏目查询子栏目
                    var childchannellist = allChannelList.FindAll(a => a.ParentId == id && a.States == 1 && a.IsIndexNav == 1);
                    if (childchannellist.Count > 0)
                    {
                        //遍历子栏目,返回查到的第一个路径不为空的栏目链接
                        for (int i = 0; i < childchannellist.Count; i++)
                        {
                            if (!string.IsNullOrEmpty(childchannellist[i].WebPath))
                            {
                                webpath = childchannellist[i].WebPath;
                                break;
                            }
                        }
                        return !string.IsNullOrEmpty(webpath) ? webpath : GetUrl(childchannellist[0].Id);
                    }
                    else
                    {
                        return defaultUrl ?? "javascript:;";
                    }
                }
                else
                {
                    return curchannel.WebPath;
                }
            }
            else
            {
                return defaultUrl ?? "javascript:;";
            }
        }


        #region 获取上下一条
        /// <summary>
        /// 获取上一条
        /// </summary>
        /// <param name="Model">当前信息的Model</param>
        /// <returns></returns>
        public dynamic GetPrveModel(dynamic Model)
        {
            /*2017.08.07 张浩修改 修改详情上一页的读取条件*/
            curChannelOrderBy = Evan.Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
            return curBLL.GetPrev(Model, curChannelOrderBy);
            //curChannelOrderBy = Evan.Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
            //return curBLL.GetPrev(Model, "ClassId=@ClassId and States=1", curChannelOrderBy, ClassId);

        }

        /// <summary>
        /// 获取下一条
        /// </summary>
        /// <param name="Model">当前信息的Model</param>
        /// <returns></returns>
        public dynamic GetNextModel(dynamic Model)
        {
            /*2017.08.07 张浩修改 修改详情下一页的读取条件*/
            curChannelOrderBy = Evan.Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
            return curBLL.GetNext(Model, curChannelOrderBy);
            //curChannelOrderBy = Evan.Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(curChannel.OrderBy), Convert.ToInt32(curChannel.SubOrderBy));
            //return curBLL.GetNext(Model, "ClassId=@ClassId and States=1", "sortid asc,addtime desc", ClassId);
        }
        #endregion

        #region 获取相册
        /// <summary>
        /// 获取相册
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <returns></returns>
        public List<Evan.Model.SysModel.PhotoModel> GetPhotoList(string JsonStr)
        {
            return Evan.Template.TemplateHelper.GetPhoto(JsonStr);
        }
        #endregion

        #region 获取列表字段
        /// <summary>
        /// 获取列表字段
        /// </summary>
        /// <param name="JsonStr"></param>
        /// <returns>dynamic列表</returns>
        public List<dynamic> GetListField(string JsonStr)
        {
            List<dynamic> list = new List<dynamic>();

            if (string.IsNullOrEmpty(JsonStr.Trim())) { return list; }

            dynamic json = Newtonsoft.Json.Linq.JToken.Parse(JsonStr) as dynamic;
            foreach (var item in json)
            {
                list.Add(item);
            }
            return list;
        }
        #endregion


        #region 方法集合

        /// <summary>
        /// 获得置顶的新闻对象集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetTopNewsByClassId(int top, string orderby, int classid)
        {
            Evan.DataBase.DbOperat BLL_Module = new Evan.DataBase.DbOperat("Module");
            List<dynamic> list = new Evan.DataBase.DbOperat("Article").GetList_1(top, "ClassId=@0 and IsTop=1 and States=1", orderby, classid);
            try
            {
                foreach (dynamic item in list)
                {
                    dynamic ServiceModel = BLL_Module.GetModel_1("Fields!='' and ChannelId=@ChannelId", item.ClassId);
                    Dictionary<string, object> MapInDictionary = Evan.Model.ConvertModel.ConvertToDictionary(item);
                    object title;
                    object intro;
                    MapInDictionary.TryGetValue("Title" + ServiceModel.ServerId, out title);
                    MapInDictionary.TryGetValue("Intro" + ServiceModel.ServerId, out intro);
                    if (title.ToString() != "")
                    {
                        item.Set("Title", title);
                    }
                    if (intro.ToString() != "")
                    {
                        item.Set("Intro", intro);
                    }
                }
            }
            catch
            {

            }
            return list;
        }

        /// <summary>
        /// 获得置顶的产品对象集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetTopProductByClassId(int top, string orderby, int classid)
        {
            Evan.DataBase.DbOperat BLL_Module = new Evan.DataBase.DbOperat("Module");
            List<dynamic> list = new Evan.DataBase.DbOperat("Product").GetList_1(top, "ClassId=@0 and IsTop=1 and States=1", orderby, classid);
            return list;
        }


        /// <summary>
        /// 获得新闻对象集合【更新前】
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetNewsByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Article").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }


        /// <summary>
        /// 获得新闻对象集合【更新后，返回对象是个list集合】
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public List<dynamic> GetArticleByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Article").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }


        /// <summary>
        /// 根据ParentId获得新闻对象集合【更新后，返回对象是个list集合】
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="parentid">当前栏目的ParentId</param>
        /// <returns></returns>
        public List<dynamic> GetArticleByParentId(int top, string orderby, int parentid)
        {
            return new Evan.DataBase.DbOperat("Article").GetList_1(top, "ClassId in (select Id from Channel where ParentId=@0) and States=1", orderby, parentid);
        }

        /// <summary>
        /// 根据ParentId获得新闻对象集合【更新后，返回对象是个list集合】
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序方式</param>
        /// <param name="parentid">当前栏目的ParentId</param>
        /// <returns></returns>
        public List<dynamic> GetArticleByParentId2(int top, string strwhere, string orderby, int parentid)
        {
            return new Evan.DataBase.DbOperat("Article").GetList_1(top, "ClassId in (select Id from Channel where ParentId=@0) and States=1 and " + strwhere, orderby, parentid);
        }


        /// <summary>
        /// 获得Banner对象集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetBannerByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Banner").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }


        /// <summary>
        /// 获得Video对象集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetVideoByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Video").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }



        /// <summary>
        /// 根据栏目ID获得Channel对象
        /// </summary>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetChannelByClassId(int classid)
        {
            return new Evan.DataBase.DbOperat("Channel").GetModel(classid);
        }


        /// <summary>
        /// 根据传进来的栏目ID获得它的子级栏目集合
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public dynamic GetSonChannelById(int channelid)
        {
            return new Evan.DataBase.DbOperat("Channel").GetList_1(0, "ParentId=@0 and States=1", "SortId asc,Id asc", channelid);
        }



        /// <summary>
        /// 根据传进来的栏目ID获得它的当前同级栏目集合
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public dynamic GetParentChannelById(int channelid)
        {
            int parentid = new Evan.DataBase.DbOperat("Channel").GetModel(channelid).ParentId;
            return new Evan.DataBase.DbOperat("Channel").GetList_1(0, "ParentId=@0 and States=1", "SortId asc,Id asc", parentid);
        }

        /// <summary>
        /// 根据传进来的栏目ID获得它的当前同级栏目集合【新】
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public dynamic GetSameChannelById(int channelid)
        {
            int parentid = new Evan.DataBase.DbOperat("Channel").GetModel(channelid).ParentId;
            return new Evan.DataBase.DbOperat("Channel").GetList_1(0, "ParentId=@0 and States=1", "SortId asc,Id asc", parentid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public int GetProductNavId(int channelid)
        {
            int navid = 0;
            dynamic parentChannel = BLL_Channel.GetModel_1("Id=@Id and States=1", channelid);
            if (parentChannel.ParentId == 0)
            {
                navid = parentChannel.Id;
            }
            else
            {
                return GetProductNavId(parentChannel.ParentId);
            }
            return navid;
        }


        /// <summary>
        /// 判断是否显示首页
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public bool IsShowIndex()
        {
            bool bo = true;
            string IsShowIndex = Evan.Common.CommFun.GetAppSetting("IsShowIndex");
            if (IsShowIndex != "1")
            {
                bo = false;
            }
            return bo;
        }


        /// <summary>
        /// 根据ClassId获得单页面的对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public dynamic GetSinglePageByClassId(int Id)
        {
            return new Evan.DataBase.DbOperat("SinglePage").GetModel_1("ClassId=@0 and States=1", Id);
        }

        /// <summary>
        /// 根据ClassId获得单页面的对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public dynamic GetRelateProductById(int top, int classid, int Id)
        {
            return new Evan.DataBase.DbOperat("Product").GetList_1(top, "ClassId=@0 and States=1 and Id <> @1", "SortId asc,AddTime asc", classid, Id);
        }

        public dynamic GetVersions()
        {
            return new Evan.DataBase.DbOperat("Versions").GetModel(1);
        }

        /// <summary>
        /// 获得方案集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public dynamic GetRelateSolutionByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Solution").GetList_1(top, "ArticleId=@0 and States=1", orderby, classid);
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Obsolete("不要再使用这个方法了 , 推荐使用 GetUrlByModel",true)]
        public string GetLocalUrl(dynamic model)
        {
            dynamic channelmodel = new Evan.DataBase.DbOperat("Channel").GetModel(model.ClassId);
            return channelmodel.WebPath + "" + model.Id + ".html";
        }

        /// <summary>
        /// 获得下载对象集合
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public List<dynamic> GetDownloadByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("DownLoad").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }

        /// <summary>
        /// 获得解决方案对象集合【更新后，返回对象是个list集合】
        /// </summary>
        /// <param name="top">前几条</param>
        /// <param name="orderby">当前栏目的排序方式</param>
        /// <param name="classid">当前栏目的Id</param>
        /// <returns></returns>
        public List<dynamic> GetSolutionByClassId(int top, string orderby, int classid)
        {
            return new Evan.DataBase.DbOperat("Solution").GetList_1(top, "ClassId=@0 and States=1", orderby, classid);
        }
        #endregion

        /// <summary>
        /// 获取网站资源版本号
        /// </summary>
        /// <returns></returns>
        public string GetStaticVersion()
        {
            string curDomain = HttpContext.Current.Request.Url.Host;

            if (Common.CommFun.GetAppSetting("DevMode") == "1" || curDomain.EndsWith("uoeee.com", StringComparison.OrdinalIgnoreCase))
            {
                return ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
            }
            else
            {
                return SiteConfig.Version;
            }
        }

        /// <summary>
        /// 获取栏目排序
        /// </summary>
        /// <param name="classId">栏目Id</param>
        /// <returns>栏目排序sql字符</returns>
        public string GetOrderBy(int classId)
        {
            return Evan.Template.TemplateHelper.GetSqlOrderStr(classId);
        }
    }
}
